/*
* This file is a part of the open-eBackup project.
* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
* If a copy of the MPL was not distributed with this file, You can obtain one at
* http://mozilla.org/MPL/2.0/.
*
* Copyright (c) [2024] Huawei Technologies Co.,Ltd.
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
*/
#ifndef BACKUP_HANDLER_H
#define BACKUP_HANDLER_H

#include "threadpool/ThreadPool.h"
#include "threadpool/ThreadPoolFactory.h"

#include "BackintConst.h"

namespace backint {
class BackupHandler {
public:
    BackupHandler();
    ~BackupHandler();

    uint32_t ExecBackup(const StuParam &inParam, const PlugParams &plugParam);
private:
    uint32_t DataBackup(const StuParam &inParam, const StuFileInfo &stFileInfo);
    uint32_t ReadPipeAndFile(const std::string &inputFile, StuFileInfo &stFileInfo);

    void CheckTaskResult(size_t &nJobs, bool &result);
    void BackupTaskInner(size_t &nJobs, bool &result);
    bool ExecutTask(size_t nJobs, const StuFileData &fileData);

    uint32_t ReadWriteDataForOra(const StuParam &inParam, const StuFileInfo &fileInfo);
    void GetBackupParam(const StuParam &inParam, const PlugParams &plugParam);

private:
    using BackupFunc = std::function<uint32_t(const StuParam&, const StuFileInfo&)>;
    std::map<std::string, BackupFunc> m_backupFunMap;
private:
    std::string m_pid;
    std::string m_jobId;
    std::string m_subJobId;
    std::string m_dbName;
    std::string m_cachePath;
    std::string m_dataPath;
    std::string m_backupID;
    std::string m_outputFile;
    int m_threadCount = 0;
    uint32_t m_channels = DEFAULT_CHANNELS;
    bool m_backupResult = true;
    std::mutex m_writeOutFileMutex;
    std::mutex m_threadMutex;

    std::shared_ptr<Module::JobScheduler> m_jsPtr;
    uint32_t m_backupTotal = 0;
    uint32_t m_backedCount = 0;
};
}

#endif